package com.twu.biblioteca.controllers;


import com.twu.biblioteca.helpers.Table;
import com.twu.biblioteca.models.Movie;
import com.twu.biblioteca.models.MovieDAO;

import java.util.ArrayList;
import java.util.List;

public final class ConsoleListMovie extends Console {

    @Override
    public void run() {
        List<Movie> movies = MovieDAO.getMoviesWithoutCheckout();

        int charsId = 2;
        int charsName = 4;
        int charsYear =  4;
        int charsDirector = 8;
        int charsRating = 6;

        for (Movie movie : movies){
            int lengthId = Long.toHexString(movie.getId()).length();
            int lengthName = movie.getName().length();
            int lengthYear = movie.getYear().toString().length();
            int lengthDirector = movie.getDirector().toString().length();
            int lengthRating = movie.getRating().toString().length();

            if (lengthId > charsId){
                charsId = lengthId;
            }
            if(lengthName > charsName){
                charsName = lengthName;
            }
            if(lengthYear > charsYear){
                charsYear = lengthYear;
            }
            if (lengthDirector > charsDirector){
                charsDirector = lengthDirector;
            }
            if (lengthRating > charsRating){
                charsRating = lengthRating;
            }
        }

        charsId += 2;
        charsName += 2;
        charsYear += 2;
        charsDirector += 2;
        charsRating += 2;

        Table table = new Table();

        ArrayList<Integer> limits = new ArrayList<Integer>();
        limits.add(charsId);
        limits.add(charsName);
        limits.add(charsYear);
        limits.add(charsDirector);
        limits.add(charsRating);
        table.setLimits(limits);

        ArrayList<String> headers = new ArrayList<String>();
        headers.add("Id");
        headers.add("Name");
        headers.add("Year");
        headers.add("Director");
        headers.add("Rating");
        table.addData(headers);

        table.addDiv();

        ArrayList<String> details = new ArrayList<String>();

        for (Movie movie : movies){
            details.add(Long.toHexString(movie.getId()));
            details.add(movie.getName());
            details.add(movie.getYear().toString());
            details.add(movie.getDirector());
            details.add(movie.getRating().toString());

            table.addData(details);

            details.removeAll(details);
        }

        this.io.getOutput().append("\n\n" + table.getTable() + "\n\n");

        new ConsoleMainMenu().run();
    }
}
